package com.vercon.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * Created by sure on 2017/11/12.
 */

public class MirrorDataStorage {
    private static final MirrorDataStorage ourInstance = new MirrorDataStorage();

    public static MirrorDataStorage instance() {
        return ourInstance;
    }

    private MirrorDataStorage() {
    }


    public void  initialize(Context context, String spName){
        LogUtils.d("======================", "MirrorDataStorage.initialize");
        this.context = context.getApplicationContext();
        this.sharedPreferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        this.editor = this.sharedPreferences.edit();
        this.gson = new Gson();
    }

    private Context context;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Gson gson;

    public <T>  T getObject(String key, Class<T> cls){
        String json  = this.sharedPreferences.getString(key, null) ;
        return gson.fromJson(json, cls);
    }
    public <T>  T getObject(String key, Type type){
        String json  = this.sharedPreferences.getString(key, null) ;
        return gson.fromJson(json, type);
    }

    public void storeObject (String key, Object obj ){
        this.editor.putString(key, new Gson().toJson(obj));
        this.editor.commit();
    }

    public void storeString (String key, String obj ){
        this.editor.putString(key, obj);
        this.editor.commit();
    }

    public void storeObjectAsync (String key, Object obj ){
        this.editor.putString(key, new Gson().toJson(obj));
        this.editor.apply();
        try {
            /* 對應斷電數據保存丟失問題 */
            Runtime.getRuntime().exec("sync");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void storeStringAsync (String key, String obj ){
        this.editor.putString(key, obj);
        this.editor.apply();
        try {
            /* 對應斷電數據保存丟失問題 */
            Runtime.getRuntime().exec("sync");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public void storeBoolean(String key, Boolean obj){
        this.editor.putBoolean(key,obj);
        this.editor.commit();
    }
    public void storeBooleanAsync(String key, Boolean obj){
        this.editor.putBoolean(key,obj);
        this.editor.apply();
        try {
            /* 對應斷電數據保存丟失問題 */
            Runtime.getRuntime().exec("sync");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public void storeInt(String key, int obj){
        this.editor.putInt(key,obj);
        this.editor.commit();
    }
    public void storeIntAsync(String key, int obj){
        this.editor.putInt(key,obj);
        this.editor.apply();
        try {
            /* 對應斷電數據保存丟失問題 */
            Runtime.getRuntime().exec("sync");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public Boolean getBoolean(String key){
        return this.sharedPreferences.getBoolean(key,false);
    }

    public Boolean getBoolean(String key, boolean defValue){
        return this.sharedPreferences.getBoolean(key,defValue);
    }

    public int getInt(String key){
        return this.sharedPreferences.getInt(key,0);
 }

    public String getString(String key){
        return this.sharedPreferences.getString(key, null) ;
    }

    public String getString(String key, String defValue){
        return this.sharedPreferences.getString(key, defValue) ;
    }

    public void removeData(String key){
        this.sharedPreferences.edit().remove(key).commit();
    }
}
